package com.sc.nft.consume;

import com.sc.nft.enums.MessageTopic;
import com.sc.nft.enums.RedisKeyEnum;
import java.util.concurrent.TimeUnit;

import com.sc.nft.service.MetaWalletService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@RocketMQMessageListener(topic = MessageTopic.TRANSFER_TO_META_WALLET + "${spring.profiles.active}", consumerGroup = MessageTopic.TRANSFER_TO_META_WALLET + "${spring.profiles.active}")
@Component
@Slf4j
@RequiredArgsConstructor
public class TransferFromMetaWalletConsumer implements RocketMQListener<Long> {

    private final RedisTemplate redisTemplate;
    private final MetaWalletService metaWalletService;
    @Override
    public void onMessage(Long userCollectionChainLogId) {

        String key = RedisKeyEnum.TRANSFER_COLLECTION_TO_META_WALLET_LOCK.getKey() + userCollectionChainLogId;
        Long increment = redisTemplate.opsForValue().increment(key);
        redisTemplate.expire(key, 120, TimeUnit.MINUTES);
        if (increment.longValue() > 1L) {
            return;
        }

        try {
            metaWalletService.transferToMetaWallet(userCollectionChainLogId);
        } catch (Exception e) {
            log.error("转出到钱包处理 记录id：{},异常:{}", userCollectionChainLogId, e);
        }

    }
}
